package com.java.web;

import com.java.dao.commDaoImpl;
import com.java.dao.userDao;
import com.java.dao.userDaoImpl;
import com.java.doMain.commodity;
import com.java.doMain.user;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author xyd
 * @Date 2021/12/23 23:05
 * @Version 1.0
 * 订单管理-查看订单
 */
public class findOrdersServlet extends HttpServlet {

    private com.java.dao.commDao commDao = new commDaoImpl();
    private com.java.dao.userDao userDao = new userDaoImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        user user = (com.java.doMain.user) request.getSession().getAttribute("user");
        if (user != null) {
            user user1 = userDao.selectByUsername(user.getUsername());
            request.getSession().setAttribute("temp", user1);
            String[] str = new String[0];
            try {
                str = user1.getB2().split(",");
            } catch (Exception e) {
            }
            System.out.println("这是数组"+Arrays.toString(str));
            List<commodity> list = new ArrayList<>();
            for (int i = 1; i < str.length; i++) {
                try {
                    if (str[i] != null || !str[i].equals("")) {
                        list.add(commDao.selectByCid(Integer.parseInt(str[i])));
                    }
                } catch (Exception e) {
                }
            }

            request.setAttribute("comm", list);
            commodity[] objects = list.toArray(new commodity[0]);

            double money = 0;
            for (int j = 0; j < objects.length; j++) {
                try{
                    money+=objects[j].getcPrice();
                }catch (Exception e){

                }
            }
            request.setAttribute("money", money);
            request.getRequestDispatcher("/admin/orders/list.jsp").forward(request,response);
        }
    }
}